翻訳と辞書
Words near each other
・ Duck Sauce
・ Duck sauce
・ Duck Season (song)
・ Duck soup
・ Duck Soup (1927 film)
・ Duck Soup (1933 film)
・ Duck soup noodles
・ Duck Soup to Nuts
・ Duck Stab!
・ Duck Stab/Buster & Glen
・ Duck Studios
・ Duck Tape'n
・ Duck test
・ Duck the Great Western Engine
・ Duck tour
Duck typing
・ Duck Ugly
・ Duck Valley Indian Reservation
・ Duck walk
・ Duck Williams
・ Duck à l'orange
・ Duck! Rabbit, Duck!
・ Duck! The Carbine High Massacre
・ Duck's ass
・ Duck's Breath Mystery Theatre
・ Duck's Cross
・ Duck's head
・ Duck's Pool, Dartmoor
・ Duck, Death and the Tulip
・ Duck, duck, goose


Dictionary Lists
翻訳と辞書 辞書検索 [ 開発暫定版 ]
スポンサード リンク

Duck typing : ウィキペディア英語版
Duck typing

In computer programming with object-oriented programming languages, duck typing is a layer of programming language and design rules on top of typing.
Typing is concerned with assigning a type to any object.
Duck typing is concerned with establishing the suitability of an object for some purpose.
With normal typing, suitability is assumed to be determined by an object's type only.
In duck typing, an object's suitability is determined by the presence of certain ''methods and properties'' (with appropriate meaning), rather than the actual type of the object.
The name of the concept refers to the duck test, attributed to James Whitcomb Riley, which may be phrased as follows:
In duck typing, a programmer is only concerned with ensuring that objects behave as demanded of them in a given context, rather than ensuring that they are of a specific class. For example, in a non-duck-typed language, one would create a function that requires that the object passed into it be of type ''Duck'', in order to ensure that that function can then use the object's ''walk'' and ''quack'' methods. In a duck-typed language, the function would take an object of any type and simply call its ''walk'' and ''quack'' methods, producing a run-time error if they are not defined. Instead of specifying types formally, duck typing practices rely on documentation, clear code, and testing to ensure correct use.
==Concept examples==
Consider the following pseudo-code for a duck-typed language:
function calculate(a, b, c) => return (a + b)
*c

example1 = calculate (1, 2, 3)
example2 = calculate ((2, 3 ), (5, 6 ), 2)
example3 = calculate ('apples ', 'and oranges, ', 3)

print to_string example1
print to_string example2
print to_string example3
In the example, each time the calculate function is called, objects without related inheritance may be used (numbers, lists and strings). As long as the objects support the "+" and "
*" methods, the operation will succeed. If translated to Ruby or Python, for example, the result of the code would be:
9
(14, 18 )
apples and oranges, apples and oranges, apples and oranges,
Thus, duck typing can work the same as polymorphism, but without inheritance. The only restriction that function calculate places on its arguments is that they implement the "+" and the "
*" methods.
The duck test can be seen in the following example (in Python). As far as the function in_the_forest is concerned, the Person object is a duck:

class Duck:
def quack(self):
print("Quaaaaaack!")
def feathers(self):
print("The duck has white and gray feathers.")
class Person:
def quack(self):
print("The person imitates a duck.")
def feathers(self):
print("The person takes a feather from the ground and shows it.")
def name(self):
print("John Smith")
def in_the_forest(duck):
duck.quack()
duck.feathers()
def game():
donald = Duck()
john = Person()
in_the_forest(donald)
in_the_forest(john)
game()


抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)
ウィキペディアで「Duck typing」の詳細全文を読む



スポンサード リンク
翻訳と辞書 : 翻訳のためのインターネットリソース

Copyright(C) kotoba.ne.jp 1997-2016. All Rights Reserved.